<template>
  <view class="jjhnndrrrt">
    <view class="hhggbxeer row bbm">
      <!-- 未审核条件 -->
      <view
        v-if="formData.status == 0"
        class="col pd ls fz26 pr mr10"
        @tap="ccaidan"
      >
        <uni-icons
          type="search"
          color="#3DADEB"
          size="24"
          class="ls cz"
        ></uni-icons>
        <text class="ml10">{{
          formData.fieldTypeId
            ? fieldTypeList.filter((i) => {
                return i.id == formData.fieldTypeId;
              })[0].name
            : "全部"
        }}</text>
        <image src="../../../static/img/down.png" class="downxeer"></image>
      </view>
      <!-- 已审核条件 -->
      <view
        v-if="formData.status == 1"
        class="col pd ls fz26 pr mr10"
        @tap="caidan"
      >
        <uni-icons
          type="search"
          color="#3DADEB"
          size="24"
          class="ls cz"
        ></uni-icons>
        <text class="ml10">{{
          formData.result ? resultList[formData.result] : "全部"
        }}</text>
        <image src="../../../static/img/down.png" class="downxeer"></image>
      </view>
       <view
        class="col pd ls fz26 pr mr10"
        @tap="unitlChange"
      >
        <text class="ml10">{{unityName ? unityName : "选择审批单位" }}</text>
        <image src="../../../static/img/down.png" class="downxeer"></image>
      </view>
      <view class="col pd ls fz26 pr cen blm">
        <picker mode="date" :value="formData.queryDate" @change="bindDateChange">
          <view class="uni-input">{{ formData.queryDate ? formData.queryDate : "选择日期" }}</view>
        </picker>
        <icon v-if="formData.queryDate" class="cancel-icon" type="cancel" size="20" color="#3DADEB" @tap="bindDateChange('')"/>
      </view>
    </view>
    <shlist
      :list="list"
      v-if="formData.status == 0"
      :fp="false"
      @click="getList(0)"
    ></shlist>
    <shlist
      :list="list"
      :fp="false"
      v-if="formData.status == 1"
      @click="getList(1)"
    ></shlist>
    <dibu :jhhbbde="jhhbbde" @call="call" :index="index"></dibu>
  </view>
</template>
<script>
import dibu from "@/components/dibu.vue";
import shlist from "../components/shlist.vue";
export default {
  onBackPress(e) {
    // 右上角的加号点击事件
    uni.switchTab({
      url: "/pages/gongzuo/index",
    });
    return true
  },
  data() {
    return {
      formData: {
        fieldTypeId: '',
        unityId:'',
        queryDate: '',
        status: 0,
        result: "",
      },
      list: [],
      unityName: '',
      index: 0,
      jhhbbde: [
        {
          name: "待审核",
          icon: "../../../static/img/dsfa_a.png",
          icons: "../../../static/img/dsfb_a.png",
        },
        {
          name: "已审核",
          icon: "../../../static/img/dsfa_b.png",
          icons: "../../../static/img/dsfb_b.png",
        },
      ],
      fieldTypeList: [],
      resultList: ["全部状态",  "已通过",  "未通过", '已撤销'],
      unityList: [],
    };
  },
  components: {
    dibu,
    shlist,
  },
  mounted() {
    console.log(this.$store.getters.getUserInfo.unityList)
    this.unityList = [{id: '', name: '全部'}].concat(this.$store.getters.getUserInfo.unityList);
    this.getFieldTypeList();
  },
  methods: {
    async init(status) {
      this.formData.status = status;
      // 已审核
      if (this.formData.status === 1) {
        let res = await this.$api.applyApproval({
          result: this.formData.result,
          unityId:this.formData.unityId,
          queryDate: this.formData.queryDate,
        });
        if (res.code == 200) {
          this.list = res.data;
        }
        // 未审核
      } else {
        let res = await this.$api.applyUnApproval({
          fieldTypeId: this.formData.fieldTypeId,
          unityId:this.formData.unityId,
          queryDate: this.formData.queryDate,
        });
        if (res.code == 200) {
          this.list = res.data;
        }
      }
    },

    bindDateChange(e) {
      if(e === '') {
        this.formData.queryDate = ''
      } else {
        this.formData.queryDate = e.detail.value;
      }
      this.init(this.formData.status);
    },

    call(e) {
      this.index = e;
      this.init(this.index);
    },

    async getFieldTypeList() {
      let res = await this.$api.fieldTypeCheck();
      this.fieldTypeList = res.data;
      this.init(0);
    },

    caidan() {
      let th = this;
      uni.showActionSheet({
        itemList: th.resultList,
        success: function (res) {
          if( res.tapIndex == 0) {
            th.formData.result =  ""
          } else if(res.tapIndex == 3) {
            th.formData.result = 0
          } else {
             th.formData.result = res.tapIndex;
          }
          th.list = [];
          th.init(1);
        },
      });
    },

    ccaidan() {
      let th = this;
      uni.showActionSheet({
        itemList: th.fieldTypeList.map((i) => {
          return i.name;
        }),
        success: function (res) {
          th.formData.fieldTypeId = th.fieldTypeList[res.tapIndex].id;
          th.list = [];
          th.init(th.formData.status);
        },
      });
    },

    unitlChange() {
      let th = this;
      uni.showActionSheet({
        itemList: th.unityList.map((i) => {
          return i.name;
        }),
        success: function (res) {
          th.formData.unityId = th.unityList[res.tapIndex].id;
          th.unityName = th.unityList[res.tapIndex].name
          th.list = [];
          th.init(th.formData.status);
        },
      });
    }
  },
};
</script>
<style scoped></style>
